<?php
include_once(dirname(__FILE__)."/../inc/common.php");
$method = isset($_GET["method"])?$_GET["method"]:"";

$flowClass = new tally_flow();

if($method=="listQry") {
    // 每年收入与支出
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $flowList = $flowClass->findFlowByUserId($userId, 0, 0, "", "",
        "", "", 0, 0, "tradeTimeAsc", 0, 1);
    $firstTradeYear = date("Y", strtotime($flowList[0]["tradeTime"]));
    $currentYear = date("Y", time());
    ?>
    <table id="listtb" class="listtable" cellpadding="0" cellspacing="1">

        <tr>
            <th width="25%">年份</th>
            <th width="25%">收入</th>
            <th width="25%">支出</th>
            <th width="25%">&nbsp;</th>
        </tr>

        <?php
        for($year=$firstTradeYear;$year<=$currentYear;$year++){
            $startTime = strtotime($year."-01-01");
            $endTime = strtotime(($year+1)."-01-01");
            $income = $flowClass->getCountByTimeRange($userId,
                tally_flow::$INOUT_INCOMME, $startTime, $endTime, $tagIds, $categoryId);
            $expense = $flowClass->getCountByTimeRange($userId,
                tally_flow::$INOUT_EXPENSE, $startTime, $endTime, $tagIds, $categoryId);
        ?>
        <tr>
            <td class="to_left"><?php echo $year; ?></td>
            <td><?php echo number_format($income, 2, ".", ""); ?></td>
            <td><?php echo number_format(abs($expense), 2, ".", ""); ?></td>
            <td></td>
        </tr>
        <?php
        }
        ?>

        <tr>
            <td colspan="13"  class="to_left">
                (注：按最新汇率,以人民币计)
            </td>
        </tr>
    </table>
    <?php
}else if($method=="pole") {
    // 每年收入与支出
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $flowList = $flowClass->findFlowByUserId($userId, 0, 0, "", "",
        "", "", 0, 0, "tradeTimeAsc", 0, 1);
    $firstTradeYear = date("Y", strtotime($flowList[0]["tradeTime"]));
    $currentYear = date("Y", time());

    $categories = array();
    $incomeList = array();
    $expenseList = array();

    for($year=$firstTradeYear;$year<=$currentYear;$year++) {
        $categories[] = $year;
        $startTime = strtotime($year . "-01-01");
        $endTime = strtotime(($year+1) . "-01-01");
        $incomeList[] = $flowClass->getCountByTimeRange($userId,
            tally_flow::$INOUT_INCOMME, $startTime, $endTime, $tagIds, $categoryId);
        $expenseList[] = abs($flowClass->getCountByTimeRange($userId,
            tally_flow::$INOUT_EXPENSE, $startTime, $endTime, $tagIds, $categoryId));
    }

    echo json_encode(array(
        "categories" => $categories,
        "series" => array(
            array(
                "name" => '收入',
                "data" => $incomeList

            ), array(
                "name" => '支出',
                "data" => $expenseList

            )
        ),
    ));
}else{
    ?>
    <!-- begin:导航条 -->
    <div class="titlebar">
        <h3>年度收支表</h3>
    </div>

    <div class="tab_wrapper">
        <div class="body">
            <!-- begin:搜索区 -->
            <form id="schForm" name="schForm" method="post">
                <div class="searchbar" id="searchbar" style="line-height: 150%; display: block;">

                    <TABLE class="complex_condition">
                        <TR>
                            <td>
                                <label for="tags">
                                    标签:
                                </label>
                                <input type="text" class="text rd" id="tags" name="tags"
                                       readonly="readonly" onClick="CM.clearValue(this);">
                                <input type="hidden" value="" name="tagId" id="tagId"/>
                                <a title="选择项目" href="#"
                                   onclick="TagPicker.open(this,'1');return false;"><img src="/images/icons/select.gif" align="absmiddle" />
                                </a>
                            </td>

                            <td>
                                <label for="categoryName">
                                    收支类别:
                                </label>
                                <input type="hidden" id="categoryId" name="categoryId" />
                                <input type="text" class="text rd" id="categoryName" name="categoryName" readonly="readonly" onClick="CM.clearValue(this);" >
                                <a title="收支类别" href="#"
                                   onclick="CategoryPicker.open(this,'');return false;"><img
                                        src="/images/icons/select.gif" align="absmiddle" />
                                </a>
                            </td>

                        </tr>


                    </TABLE>

                </div>
            </form>
            <!-- end:搜索区 -->

            <div class="report_bar">
                <span>数据列表</span>
            </div>

            <div id="dataView" class="data">
                <div align="center">
                    <img align="absmiddle" src='/images/common/loading1.gif'>
                </div>
            </div>


            <div class="report_bar">
                <span>数据图表</span>
            </div>

            <div id="chartView" class="chartView">
                <div  align="center"><img align="absmiddle" src='/images/common/loading1.gif'></div>
            </div>
            <!-- end:---------主数据区 -->
        </div>
    </div>
    <?php
}
?>
